﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

using SharedGenomics.Workbench.Annotations;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    public class AnnnotationContextMenuRenderer : ToolStripProfessionalRenderer
    {
        private static Bitmap _finishedBm = CreateStatusBitmap(Color.Green);
        private static Bitmap _pendingBm = CreateStatusBitmap(Color.Orange);
        private static Bitmap _failedBm = CreateStatusBitmap(Color.Red);

        protected override void OnRenderItemImage(ToolStripItemImageRenderEventArgs e)
        {
            base.OnRenderItemImage(e);

            var annotationMenuItem = e.Item as AnnotationContextMenuItemView;
            if (annotationMenuItem != null)
            {
                Graphics g = e.Graphics;
                Bitmap bmp = GetBitmapForStatus(annotationMenuItem.Status);
                g.DrawImage(bmp, e.ImageRectangle);
            }
        }

        protected override void OnRenderItemBackground(ToolStripItemRenderEventArgs e)
        {
            base.OnRenderItemBackground(e);
        }

        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            base.OnRenderItemText(e);
        }

        protected override void InitializeItem(ToolStripItem item)
        {
            base.InitializeItem(item);
            item.Image = _finishedBm;
        }

        private static Bitmap CreateStatusBitmap(Color colour)
        {
            Bitmap bmp = new Bitmap(32, 32);
            Graphics g = Graphics.FromImage(bmp);

            Rectangle colourRect = new Rectangle(2, 2, 28, 28);
            using (Brush brush = new SolidBrush(colour))
            {
                g.FillRectangle(brush, colourRect);
            }

            using (Pen pen = new Pen(Color.Black, 2.0f))
            {
                g.DrawRectangle(pen, 0, 0, 30, 30);
            }

            return bmp;
        }

        private static Bitmap GetBitmapForStatus(AnnotationRequestStatus status)
        {
            switch (status)
            {
                case AnnotationRequestStatus.Failed: return _failedBm;
                case AnnotationRequestStatus.Finished: return _finishedBm;
                default: return _pendingBm;
            }
        }
    }
}
